<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of cdkeyview
 *
 * @author Dragon.hu add @2015.12.3
 */
defined('BASEPATH') or die('forbidden');

class Cdkeyview_Controller extends Controller {

	private $tmpconnect;	//数据库连接
	private $channels;      //渠道列表
	private $keytypeList;   //礼包类型列表
    private $giftbagid;
    //private $cdkeytype;

	function _init() {
		check_level(50, segment(0), segment(1), segment(2, 'index'));
		//set_time_limit(0);

        $this->tmpconnect = $this->load->database('db') or die("load failed!".mysql_error());
        $this->tmpconnect->query("SELECT * FROM tt_channel");
        while ($n = $this->tmpconnect->get_array('default')) {
            $this->channels[$n['id']] = $n['channel'];
        }

		/*$this->keytypeList = array('1' => '微信关注礼包',
				'2' => '排名礼包', '3' => '渠道礼包1', '4' => '渠道礼包2', '5' => '预留',
				'6' => '集赞19礼包', '7' => '集赞38礼包', '8' => '集赞68礼包', '9' => '集赞98礼包'
		);*/
        // account数据库
	//$this->tmpconnect = $this->load->database('db') or die("load failed!".mysql_error());
        //$this->tmpconnect->query("SELECT * FROM cdkey_type");
        $this->tmpconnect->query("SELECT * FROM dynamic_type_object");
        while($n = $this->tmpconnect->get_array('default'))
        {
            //$this->cdkeytype[$n['type']] = $n;
            //$this->keytypeList[$n['type']] = $n['name'];

            $this->keytypeList[$n['id']] = $n['name'];
            $this->giftbagid[$n['id']] = $n['id'];
        }
	}

	function index() {
		$this->view->display("cdkey/cdkeyview/index");
	}

	function flexigrid() {
		list($colkey, $colsinfo, $where, $sortname, $sortorder, $offset, $rp, $page) = $this->common->get_flexigrid_params();

		$sql = "SELECT COUNT(*) AS num FROM cdkey WHERE {$where}"; 
		
		$one = $this->tmpconnect->one($sql);
		$total = $one['num'];
		$orderby = $sortname ? "ORDER BY {$sortname} {$sortorder} " : "";

        $colsinfo2=str_replace("`keyChar`","concat('\t',`keyChar`,'11') as `keyChar`",$colsinfo);
		$sql = "SELECT {$colsinfo2} FROM cdkey where {$where} {$orderby} LIMIT {$offset},{$rp}";

		$this->tmpconnect->query($sql);
		
		while ($n = $this->tmpconnect->get_array('default'))
		{
			//$n['channel'] = "(".$n['channel'].")".$this->channels[$n['channel']];
			//$n['keytype'] = "(".$n['keytype'].")".$this->keytypeList[$n['keytype']];
			//$n['channel'] = $this->channels[$n['channel']];
			if ($n['isuse'] == 2){
				$n['isuse'] = "已领取";
			}else{

            	date_default_timezone_set("PRC");
            	$starttime = strtotime($n['starttime']);
            	$endtime = strtotime($n['endtime']);
            	$nowtime = time();
            	if ($starttime > $nowtime ) {
            		$n['isuse'] = "时间未到";
            	} else if ($endtime < $nowtime) {
            		$n['isuse'] = "过期";
            	}else{
            		$n['isuse'] = "未领取";
            	}
            	$n['keyChar']=substr($n['keyChar'],0,-2);
			}

			$rows[] = array('id' => $n['id'], 'cell' => $n);
		}
		
		$result = array(
	            'page' => $page,
	            'total' => $total,
	            'rows' => $rows
		);

		//$resultJson=json_encode($result,JSON_PARTIAL_OUTPUT_ON_ERROR);
        //$err=json_last_error();
		//echo $resultJson;
		echo json_encode($result);
	}
	
	function multi() {
		//print_r($_POST);
		$type = $this->input->post("type");
		$ids = $this->input->post("ids");
		$c = 0;
		$r = '';
		//echo " type = $type ";
		//echo " ids = $ids ";
		if (count(explode(',', $ids)) == 0) {
			$r = translate('C_INVALID_PARAMS');
		} else
		{
			if ($type == 'delete') {
				$sql = "DELETE FROM cdkey WHERE id IN ({$ids})";
				//echo " sql = $sql ";
				$this->tmpconnect->query($sql);
				$c = 1;
			} else
			{
				$r = translate('C_INVALID_PARAMS');
			}
		}
		json_output($c, $r);
	
	}

}
